package com.blb.community_admin.controller;

import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.*;
import com.blb.community_admin.service.IRoleService;
import com.blb.community_admin.service.ISysUserPostService;
import com.blb.community_admin.service.ISysUserRoleService;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags ="添加userRole")
public class SysUserRoleController {

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @Resource
    private IRoleService iRoleService;

    @Log(module = "用户角色管理", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加用户Role")
    @PostMapping("/users/addrole")
    public ResponseResult<String> addpost(Long roleId,@RequestBody SysUser sysUser){
        SysUserRole sysUserRole = new SysUserRole(sysUser.getUserId(),roleId);
        sysUserRoleService.save(sysUserRole);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("查询用户post")
    @GetMapping("/users/roleid/{userId}")
    public ResponseResult<SysRole> byRoleId(@PathVariable Long userId){
        SysUserRole sysUserRole = sysUserRoleService.getById(userId);
        SysRole sysRole = iRoleService.getById(sysUserRole.getRoleId());
        return ResponseResult.ok(sysRole);
    }

    @Log(module = "用户角色管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("根据userid修改roleid")
    @PutMapping("/users/updaterole/{role}")
    public ResponseResult<String> updateUser(@PathVariable Long role,@RequestBody SysUser sysUser){
        SysUserRole sysUserRole = new SysUserRole(sysUser.getUserId(), role);
        sysUserRoleService.updateById(sysUserRole);
        return ResponseResult.ok("修改成功");

    }

}
